package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeServiceImpl;

    @PostMapping("/login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee) {
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.lambda().eq(Employee::getUsername, employee.getUsername());
        Employee EmployeeFromDatabase = employeeServiceImpl.getOne(employeeQueryWrapper);
        if (EmployeeFromDatabase == null) {
            return R.error("用户不存在");
        }
        String MD5Password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!MD5Password.equals(EmployeeFromDatabase.getPassword())) {
            return R.error("密码错误");
        }
        if (0 == EmployeeFromDatabase.getStatus()) {
            return R.error("用户被禁用");
        }
        session.setAttribute("employee", EmployeeFromDatabase.getId());
        return R.success(EmployeeFromDatabase);
    }

    @PostMapping("/logout")
    public R<Object> logout(HttpSession session) {
        try {
            session.invalidate();
        } catch (Exception exception) {
            return R.error("退出失败！");
        }
        return R.success(null);
    }

    @PostMapping
    public R<String> addEmployee(HttpSession session, @RequestBody Employee employee) {
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        Long id = (Long) session.getAttribute("employee");
        employee.setCreateTime(LocalDateTime.now());
        employee.setCreateUser(id);
        employee.setUpdateUser(id);
        employeeServiceImpl.save(employee);
        return R.success("新增员工成功");

    }

    @GetMapping("/page")
    public R<Page> pageQueryEmployee(@RequestParam("page") Integer page,
                               @RequestParam("pageSize") Integer pageSize,
                               @RequestParam(value = "name", required = false) String name) {
        Page<Employee> pageConfig = new Page<>(page, pageSize);
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.lambda().like(StringUtils.isNotEmpty(name), Employee::getName, name);
        employeeQueryWrapper.lambda().orderByDesc(Employee::getUpdateTime);
        employeeServiceImpl.page(pageConfig, employeeQueryWrapper);
        return R.success(pageConfig);
    }

    @PutMapping
    public R<Employee> updateEmployee(HttpSession session,@RequestBody Employee employee){
        Long id =(Long) session.getAttribute("employee");
        employee.setUpdateUser(id);
        employeeServiceImpl.updateById(employee);
        return R.success(employee);
    }

    @GetMapping("/{id}")

    public R<Employee> getEmployeeById(@PathVariable Long id){
        Employee employee = employeeServiceImpl.getById(id);
        return employee==null?R.error("查无此人"):R.success(employee);
    }

}
